package org.example.forum.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.config.AppConfig;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.User;
import org.example.forum.service.IUserService;
import org.example.forum.utils.MD5Util;
import org.example.forum.utils.UUIDUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    IUserService userService;
    @PostMapping("/register")
    public AppResult register(String username, String password, String passwordRepeat,String nickname) {
        if(!password.equals(passwordRepeat)) {
             return  AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)||StringUtils.isEmpty(nickname)) {
            return AppResult.failed(ResultCode.ERROR_IS_NULL);
        }
        User user = new User();
        user.setNickname(nickname);
        user.setUsername(username);
        user.setPassword(password);
        String salt= UUIDUtil.UUID_32();
        user.setSalt(salt);
        String miwen=MD5Util.md5Salt(password,salt);
        user.setPassword(miwen);
        userService.createNormalUser(user);
        //把User设置到Session中

        return AppResult.success();
    }
    @PostMapping("/login")
    public AppResult login(HttpServletRequest request,String username, String password) {
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)) {
            return AppResult.failed(ResultCode.ERROR_IS_NULL);
        }
        User user=userService.selectByUserName(username);
        if(user==null) {

            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        String mingwen=MD5Util.md5Salt(password,user.getSalt());
        if(!mingwen.equals(user.getPassword())) {
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME.toString());
        }
        HttpSession session=request.getSession(true);
        session.setAttribute(AppConfig.USER_SESSION,user);
        log.info("登录成功 "+username);
        return AppResult.success();
    }
    @GetMapping("/logout")
    public AppResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session!=null){
            session.invalidate();
        }
        log.info("有客户端退出 ");
        return AppResult.success();
    }
    @GetMapping("/info")
    public AppResult<User> info(HttpServletRequest request, @RequestParam(value = "id", required = false) Long id ){
        User user=null;
    if(id==null) {
       HttpSession session = request.getSession();
       user=(User)session.getAttribute(AppConfig.USER_SESSION);
    }else{
        user=userService.selectById(id);
    }
    return AppResult.success(user);
    }
    @PostMapping("/modifyInfo")
    public AppResult modifyInfo(HttpServletRequest request, @RequestParam(value = "username", required = false) String username
            , @RequestParam(value = "gender", required = false) Byte gender
            , @RequestParam(value = "email", required = false) String email
            , @RequestParam(value = "phoneNum", required = false) String phoneNum
            , @RequestParam(value = "nickname", required = false) String nickname
             ,@RequestParam(value = "remark",required = false) String remark) {
        if(StringUtils.isEmpty(username)&&StringUtils.isEmpty(nickname)
                &&StringUtils.isEmpty(email)&&StringUtils.isEmpty(phoneNum)&&StringUtils.isEmpty(phoneNum)&&gender==null) {
            return AppResult.failed("请输入要修改的参数 ");
        }

        HttpSession session = request.getSession(false);
        User user=(User)session.getAttribute(AppConfig.USER_SESSION);
        User updateUser=new User();
       updateUser.setUsername(username);
        System.out.println(username);
        updateUser.setGender(gender);
        updateUser.setEmail(email);
        updateUser.setPhoneNum(phoneNum);
        updateUser.setNickname(nickname);
        updateUser.setId(user.getId());
        updateUser.setRemark(remark);

        userService.modifyInfo(updateUser);
        //改变用户信息，重新设置session
        user = userService.selectById(user.getId());
        // 6. 把最新的用户信息设置到session中
        session.setAttribute(AppConfig.USER_SESSION, user);
        return AppResult.success();
    }
    @PostMapping("/modyPwd")
    public AppResult modyPwd(HttpServletRequest request, String oldPassword, String newpassword,String username) {
        if(!oldPassword.equals(newpassword)) {
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(newpassword)) {
            return AppResult.failed(ResultCode.ERROR_IS_NULL);
        }
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 3.\调⽤Service
        userService.modifyPassword(user.getId(), newpassword, oldPassword);
        // 4.销session
        if (session != null) {
            session.invalidate();
        }
        // 5.返回结果
        user=userService.selectById(user.getId());
        session.setAttribute(AppConfig.USER_SESSION,user);
        return AppResult.success();
    }
}
